package com.loyal.camerax.ui.settings.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.preference.ListPreference
import androidx.preference.PreferenceFragmentCompat
import com.loyal.camerax.R
import com.loyal.camerax.ui.CameraViewModel
import com.loyal.camerax.utils.PreferenceHelper

class SettingsFragment : PreferenceFragmentCompat() {
    private var orientationPreference: ListPreference? = null
    private var captureGravityPreference: ListPreference? = null
    private var flashPreference: ListPreference? = null

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences_camerax, rootKey)
        initPreference()
        initValue()
    }

    private fun initPreference() {
        orientationPreference = findPreference(getString(R.string.orientation_key))
        captureGravityPreference = findPreference(getString(R.string.gravity_key))
        flashPreference = findPreference(getString(R.string.flash_key))

        updateUIGravity(orientationPreference?.value)
    }

    private fun initValue() {
        val mutableOrientation =
            activity?.intent?.getBooleanExtra(PreferenceHelper.MUTABLE_ORIENTATION, false)
                ?: false
        //orientationPreference?.isEnabled = enableOrientation
        orientationPreference?.isVisible = mutableOrientation

        val oldOrientationValue = orientationPreference?.value
        orientationPreference?.setOnPreferenceChangeListener { _, newValue ->
            updateUIGravity(newValue)

            val intent = if (oldOrientationValue != newValue) {
                Intent(PreferenceHelper.KEY_RESTART_ACTION)
            } else
                null
            update(intent)
            true
        }

        val oldGravityValue = captureGravityPreference?.value
        captureGravityPreference?.setOnPreferenceChangeListener { _, newValue ->
            val intent = if (oldGravityValue != newValue) {
                Intent(PreferenceHelper.KEY_RESTART_ACTION)
            } else null

            update(intent)
            true
        }

        val oldFlashValue = flashPreference?.value
        flashPreference?.setOnPreferenceChangeListener { _, newValue ->
            val intent = if (oldFlashValue != newValue) {
                Intent(PreferenceHelper.KEY_CHANGED_ACTION)
            } else null

            update(intent)
            true
        }
    }

    /**根据屏幕方向*/
    private fun updateUIGravity(newValue: Any?) {
        val landEnable = getString(R.string.orientation_value_land) == newValue
        captureGravityPreference?.isVisible = landEnable
        captureGravityPreference?.isEnabled = landEnable
    }

    private fun update(intent: Intent?) {
        cameraViewModel.backToReceiver(intent)
    }

    private val cameraViewModel: CameraViewModel by activityViewModels()

    override fun onPause() {
        super.onPause()
        cameraViewModel.updateStatus(javaClass.hashCode())
    }

    override fun onResume() {
        super.onResume()
        cameraViewModel.updateStatus(javaClass.hashCode())
    }
}